package com.tiancheng.onecode.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiancheng.onecode.entity.Role;
import com.tiancheng.onecode.entity.User;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.model.request.BasePageReq;
import com.tiancheng.onecode.model.request.admin.MerchantUserQueryReq;
import com.tiancheng.onecode.model.request.admin.PlatformMerchantUserAddReq;
import com.tiancheng.onecode.model.response.PageInfo;
import com.tiancheng.onecode.model.response.Result;
import com.tiancheng.onecode.model.response.admin.MerchantUserQueryRes;
import com.tiancheng.onecode.service.IRoleService;
import com.tiancheng.onecode.service.IUserService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.simpleframework.xml.Path;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: kellen
 * @Description: B端角色管理admin controller
 * @Date: create in 2024/12/12 16:47
 */
@Controller
@RequestMapping("/admin/role")
public class RoleAdminController {

    @Resource
    private IRoleService roleService;

    @GetMapping(value = "/queryPage")
    @ResponseBody
    public Result<PageInfo<Role>> queryPage(BasePageReq params) {
        final PageInfo<Role> page = roleService.queryPage(params);
        return Result.success(page);
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestBody Role role) {
        Asserts.isTrue(StringUtils.isNotEmpty(role.getRoleName()),"角色名为空！");
        Asserts.isTrue(StringUtils.isNotEmpty(role.getRoleName()),"角色备注为空！");
        final Role role1 = roleService.getOne(new LambdaQueryWrapper<Role>().eq(Role::getRoleName, role.getRoleName()));
        if(Objects.nonNull(role.getId())){
            //修改
            if(role.getRoleName().equals(role1.getRoleName())){
                Asserts.isTrue(role.getId().equals(role1.getId()),"角色名称重复！");
            }
            roleService.updateById(role);
        }else{
            // 添加
            Asserts.isTrue(role1.getRoleName().equals(role.getRoleName()),"角色名称重复！");
            roleService.save(role);
        }
        return Result.success();
    }


    @DeleteMapping(value = "/")
    @ResponseBody
    public Result delete(@PathVariable Long id) {
        Boolean result= roleService.delete(id);
        return Result.success();
    }
}
